﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EventConsoleApplication1
{
    public class First
    {
        public static void Run()
        {
            Machine machine = new Machine();

            IWorker busyWorker = new BusyWorker();
            busyWorker.Name = "Busy Worker";

            IWorker idleWorker = new IdleWorker();
            idleWorker.Name = "Idle Worker";

            machine.MachineBrokenEvent += busyWorker.DoJob;
            machine.MachineWorkWellEvent += idleWorker.DoJob;

            ///////////////////////////////////////////////////////////////////////////////////////////
            // 下面开始验证是否可以为一个事件添加多个订阅者
            IWorker busyWorker2 = new IdleWorkerC();
            busyWorker2.Name = "Busy Worker 2";
            machine.MachineBrokenEvent += busyWorker2.DoJob;
            // 试验结果是可以的
            // 同样可以使用静态委托，静态委托的好处之一是节省了内存空间
            ///////////////////////////////////////////////////////////////////////////////////////////
            machine.IsWorkWell = true;
            machine.Run();  // 机器开始运作
        }
    }
    /**
11      * 机器Machine为被订阅者，维修工Repairer为订阅者
12      * 工人（订阅者）注册机器的不同事件，并注册回调函数以当相应事件发生时进行处理
13      * 不同的工人可以只关注他们各自感兴趣的事件
14      */
    // 机器类
    public class Machine
    {
        // 机器是否正常工作状态
        public bool IsWorkWell { get; set; }

        // 机器故障事件委托
        public delegate void MachineBroken(string brokenMsg);
        public event MachineBroken MachineBrokenEvent;

        // 机器正常工作事件委托
        public delegate void MachineWorkWell(string msg);
        public event MachineWorkWell MachineWorkWellEvent;

        // 机器运作
        public void Run()
        {
            //IsWorkWell = false; // 设置机器出故障

            if (IsWorkWell)
            {
                if (MachineBrokenEvent != null) // 有订阅者注册机器故障事件
                {
                    MachineBrokenEvent("故障");
                }
            }
            else
            {
                if (MachineWorkWellEvent != null) // 有订阅者注册机器运行正常事件
                {
                    MachineWorkWellEvent("正常");
                }
            }
        }
    }

    public interface IWorker
    {
        string Name { get; set; }
        void DoJob(string msg);
    }

    // BusyWorker注册机器故障事件
    public class BusyWorker : IWorker
    {
        public string Name { get; set; }

        public void DoJob(string msg)
        {
            Console.WriteLine(string.Format("{0}： < {1} > DoJobA TODO", Name, msg));
        }
    }

    // IdleWorker注册机器正常工作事件
    public class IdleWorker : IWorker
    {
        public string Name { get; set; }

        public void DoJob(string msg)
        {
            Console.WriteLine(string.Format("{0}:  < {1} > DoJobB TODO", Name, msg));
        }
    }

    // IdleWorker注册机器正常工作事件
    public class IdleWorkerC : IWorker
    {
        public string Name { get; set; }

        public void DoJob(string msg)
        {
            Console.WriteLine(string.Format("{0}:  < {1} > DoJobC TODO", Name, msg));
        }
    }
}


